package win.larryzeal.c01;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.Authorizer;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;
import org.junit.Test;
import win.larryzeal.c01.realm.MyJdbcRealm;

/**
 * TODO 新手务必先理解C01_basic中的三个test！
 * 因为JdbcRealm的作用仅仅是设置了从jdbc中获取值来判断。
 * <p>
 * Created by 张少昆 on 2017/11/18.
 */
public class C01_jdbc {

    // 有一点需要注意，这里是用了代码方式。也可以使用配置方式，即只ini文件中设置。
    @Test
    public void r1(){
        DefaultSecurityManager securityManager = new DefaultSecurityManager(new MyJdbcRealm());
        SecurityUtils.setSecurityManager(securityManager);
        // securityManager.authenticate();
        // securityManager.getAuthenticator();//事实上已经有了认证器，而且是多模的，org.apache.shiro.mgt.AuthenticatingSecurityManager.AuthenticatingSecurityManager
        // 以上，如果不想用默认的，均可以自行设置

        Subject subject = SecurityUtils.getSubject();
        subject.login(new UsernamePasswordToken("zhang", "123"));
        // subject.login(new UsernamePasswordToken("zhangsan","123")); //org.apache.shiro.authc.UnknownAccountException: No account found for user [zhangsan]
        // subject.login(new UsernamePasswordToken("zhang", "456"));//org.apache.shiro.authc.IncorrectCredentialsException: Submitted credentials for token [org.apache.shiro.authc.UsernamePasswordToken - zhang, rememberMe=false] did not match the expected credentials.

        System.out.println(subject.isAuthenticated());
    }

    //多线程传播，因为subject是绑定到线程的！
    @Test
    public void r2(){
        Subject subject;
        // subject.execute(runnable);
        // subject.associateWith(runnable)
    }
}
